<template>
  <div class="app-container" v-loading="loading">
    <div class="action-container">
      <el-button icon="el-icon-plus" size="small" type="primary" @click="openCreatedDialog">添加打印机</el-button>
      <el-button icon="el-icon-delete" size="small" @click="deleteprinters">批量删除</el-button>
      <el-button icon="el-icon-refresh" size="small" @click="refresh">刷新</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="name"
        label="打印机名称">
      </el-table-column>
      <el-table-column
        prop="printType"
        label="打印机类型">
        <template slot-scope="scope">
          <template v-if="scope.row.printType===1">飞鹅打印机</template>
          <template v-else-if="scope.row.printType===2">网口/WIFI打印机</template>
          <template v-else-if="scope.row.printType===3">USB打印机</template>
          <template v-else-if="scope.row.printType===4">零距打印机</template>
        </template>
      </el-table-column>
      <el-table-column
        prop="printSnCode"
        label="IP地址/SN码">
      </el-table-column>
      <el-table-column
        prop="printKey"
        label="端口号/打印机KEY">
      </el-table-column>
      <el-table-column
        prop="printWide"
        label="打印纸宽度">
        <template slot-scope="scope">
          <template v-if="scope.row.printWide===1">80mm</template>
          <template v-else-if="scope.row.printWide===2">58mm</template>
        </template>
      </el-table-column>
      <el-table-column
        prop="waiting"
        label="待打印">
      </el-table-column>
      <el-table-column
        prop="isDefault"
        label="默认打印机">
        <template slot-scope="scope">
          <template v-if="scope.row.isDefault===1">是</template>
          <template v-else-if="scope.row.isDefault===2">否</template>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间">
      </el-table-column>
      <el-table-column
        label="编辑"
        align="left" min-width="200">
        <template slot-scope="scope">
          <el-button style="padding: 7px 27px;margin-left: 10px;margin-bottom: 3px" size="mini" type="primary"
                     icon="el-icon-edit" @click="openEditDialog(scope.row)">编辑
          </el-button>
          <template v-if="scope.row.isDefault===1">
          <el-button style="padding: 7px 27px;margin-left: 10px;margin-bottom: 3px" size="mini" type="primary"
                     icon="el-icon-edit" @click="setDefaultPrinter(scope.row)">取消默认
          </el-button>
          </template>
          <template v-if="scope.row.isDefault===2">
            <el-button style="padding: 7px 27px;margin-left: 10px;margin-bottom: 3px" size="mini" type="primary"
                       icon="el-icon-edit" @click="setDefaultPrinter(scope.row)">设为默认
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total-elements="totalElements"
      :change-callback="getprinterList"
      ref="page"></pagination>
    <el-dialog
      title="添加打印机"
      :visible.sync="createdDialog"
      :before-close="closeCreatedDialog">
      <el-form :model="form">
        <el-form-item label="打印机名称:" :label-width="formLabelWidth">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="打印机类型" :label-width="formLabelWidth">
          <el-radio-group v-model="form.printType">
            <el-radio :label="1">飞鹅打印机</el-radio>
            <el-radio :label="2">网口/WIFI打印机</el-radio>
            <el-radio :label="3">USB打印机</el-radio>
            <el-radio :label="4">零距打印机</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="打印机SN码:" :label-width="formLabelWidth">
          <el-input v-model="form.printSnCode"></el-input>
        </el-form-item>
        <el-form-item label="打印机Key:" :label-width="formLabelWidth">
          <el-input type="text" v-model="form.printKey"></el-input>
        </el-form-item>
        <el-form-item label="打印机宽度" :label-width="formLabelWidth">
          <el-radio-group v-model="form.printWide">
            <el-radio :label="1">80mm</el-radio>
            <el-radio :label="2">58mm</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeCreatedDialog">取 消</el-button>
        <el-button type="primary" @click="addprinter" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="编辑信息"
      :visible.sync="editDialog"
      :before-close="closeEditDialog">
      <el-form :model="currentRow">
        <el-form-item label="打印机名称:" :label-width="formLabelWidth">
          <el-input v-model="currentRow.name"></el-input>
        </el-form-item>
        <el-form-item label="打印机类型" :label-width="formLabelWidth">
          <el-radio-group v-model="currentRow.printType">
            <el-radio :label="1">飞鹅打印机</el-radio>
            <el-radio :label="2">网口/WIFI打印机</el-radio>
            <el-radio :label="3">USB打印机</el-radio>
            <el-radio :label="4">零距打印机</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="打印机SN码:" :label-width="formLabelWidth">
          <el-input v-model="currentRow.printSnCode"></el-input>
        </el-form-item>
        <el-form-item label="打印机Key:" :label-width="formLabelWidth">
          <el-input type="text" v-model="currentRow.printKey"></el-input>
        </el-form-item>
        <el-form-item label="打印机宽度" :label-width="formLabelWidth">
          <el-radio-group v-model="currentRow.printWide">
            <el-radio :label="1">80mm</el-radio>
            <el-radio :label="2">58mm</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeEditDialog">取 消</el-button>
        <el-button type="primary" @click="editprinter" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {printerList, printerAdd, printerUpdate, printerLogicalDel,setDefaultPrinter} from '../api/printer'
  import pagination from '@/components/pagination/index'

  export default {
    name: 'printer',
    data() {
      return {
        tableData: [], // 表格数据
        totalElements: 0, // 数据条数
        multipleSelection: [], // 选中数据-多选
        loading: true, // loading效果
        createdDialog: false, // 打印机模态框
        formLabelWidth: '120px',
        printerRoomOptions: [],
        printerTypeOptions: [],
        query: {
          printerRoomId: '',
          printerTypeId: '',
        },
        specListForm: {
          list: [],
        },
        form: {
          name: '',
          printType: 1,
          printSnCode: '',
          printKey: '',
          printWide: 1,
          waiting: 0,
          isDefault: 2,
          storeId: sessionStorage.getItem("storeId")
        }, // 打印机数据
        btnLoading: false, // 按钮loading
        editDialog: false, // 编辑模态框,
        currentRow: {
          id: '',
          name: '',
          printType: '',
          printSnCode: '',
          printKey: '',
          printWide: ''
        },
        headers: {
          accessToken: sessionStorage.token
        } // 请求头部
      }
    },
    components: {pagination},
    created() {
      this.getprinterList()
    },
    methods: {
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleDelete(row) {
        this.multipleSelection.push(row)
        this.deleteprinters()
      },


      /*
      * 获取打印机列表
      * */
      getprinterList: function (number, pageSize) {
        this.loading = true
        printerList(number, pageSize, this.query).then(response => {
          let data = response.obj
          console.log(data)
          this.totalElements = data.totalElements
          this.tableData = data.content
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      },
      /*
      * 刷新
      * */
      refresh() {
        this.$refs.page.refresh()
      },
      /*
      * 打开打印机模态框
      * */
      openCreatedDialog() {
        this.createdDialog = true
      },
      /*
      * 关闭打印机模态框
      * */
      closeCreatedDialog() {
        this.createdDialog = false
        this.form = {
          name: '',
          printType: 1,
          printSnCode: '',
          printKey: '',
          printWide: 1,
          waiting: 0,
          isDefault: 2,
          storeId: '1'
        } // 打印机数据

      },
      /*
      * 添加打印机
      * */
      addprinter() {
        this.loading = true
        this.btnLoading = true
        console.log(this.form)
        printerAdd(this.form).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.closeCreatedDialog()
          this.getprinterList()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },
      /*
      * 打开编辑模态框
      * */
      openEditDialog(row) {
        console.log(row)
        this.currentRow.id = row.id
        this.currentRow.name = row.name
        this.currentRow.printType = row.printType
        this.currentRow.printSnCode = row.printSnCode
        this.currentRow.printKey = row.printKey
        this.currentRow.printWide = row.printWide
        this.editDialog = true
      },
      /*
      * 关闭编辑模态框
      * */
      closeEditDialog() {
        this.editDialog = false
      },
      /*
      * 编辑
      * */
      editprinter() {
        this.loading = true
        this.btnLoading = true;
        printerUpdate(this.currentRow).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.loading = false;
          this.closeEditDialog()
          this.refresh()
        }).catch(() => {
          this.loading = false
          this.btnLoading = false
        })
      },
      /*
      * 删除打印机
      * */
      deleteprinters() {
        this.$confirm('是否删除这些打印机？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.multipleSelection.length) {
            this.loading = true
            let ids = []
            this.multipleSelection.forEach((item) => {
              ids.push(item.id)
            })
            printerLogicalDel(ids).then(response => {
              this.$message({
                message: response.msg,
                type: 'success',
                duration: 1000
              })
              this.multipleSelection = []
              this.$refs.page.refresh()
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.$message({
              message: '请选择打印机',
              type: 'warning',
              duration: 1000
            })
          }
        })
      },
      //设置默认打印机
      setDefaultPrinter(row) {
        this.$confirm('是否确认操作？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          setDefaultPrinter(row.id).then(response => {
            this.$message({
              message: response.msg,
              type: 'success',
              duration: 1000
            })
            this.$refs.page.search()
          }).catch(() => {
            this.loading = false
          })
        })
      },

      //重置
      reset() {
        this.query = {
          printerTypeId: '',
          printerRoomId: '',
        }
        this.$refs.page.search()
      },
      /*
      * 搜索
      * */
      search() {
        this.$refs.page.search()
      },
    }
  }
</script>

<style scoped>
  .logo-uploader {
    display: inline-block;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .logo-uploader:hover {
    border-color: #409EFF;
  }

  .logo-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .logo {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
